package com.aspire.boc.mms.wmf.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aspire.boc.mms.wmf.model.SecurityUser;
import com.aspire.boc.mms.wmf.model.WmfRole;
import com.aspire.boc.mms.wmf.service.PermissionService;

@Controller
public class RoleAct extends BaseAct {
	private static Log log = LogFactory.getLog(RoleAct.class);
	@RequestMapping(value = "/role/index")
	public String index(HttpServletRequest request, HttpServletResponse response ,HttpSession session) {
		log.info(">>>>>>>> /role/index");
		return "../views/wmf/role/index.jsp";
	}
	
	
	/**
	 * 
	 * @Title: list 
	 * @Description: TODO(角色列表,根据组获取角色列表) 
	 * @param start
	 * @param limit
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/role/list") 
	public @ResponseBody HashMap<String, Object> list(
			@RequestParam(value = "clickGroupId", required = false) String clickGroupId,
			@RequestParam(value = "start", required = true) int start,
			@RequestParam(value = "limit", required = true) int limit,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			log.info(">>>>>>>>  /role/list" + start + "--->" + limit);
			SecurityUser securityUser = (SecurityUser) request.getSession().getAttribute("securityUser");
			boolean isAdmin = securityUser.isSystemAdmin();
			if(clickGroupId == null||clickGroupId.equals("")){
				if(isAdmin){
					List<WmfRole> roles =  permissionService.getRolesPage(null,start, limit);
					long total = permissionService.getRolesCount(null);
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("total", total);
					map.put("roles", roles);
					return map;
				}else{
					List<String> groupIdLists = (List<String>) request.getSession().getAttribute("groupIds");
					String groupIds = "";
					for(String groupId : groupIdLists){
						groupIds = groupIds + groupId + ",";
					}
					if(groupIds.endsWith(",")){
						groupIds.substring(0,groupIds.length()-1);
					}
					List<WmfRole> roles =  permissionService.getRolesPage(groupIds,start, limit);
					long total = permissionService.getRolesCount(clickGroupId);
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("total", total);
					map.put("roles", roles);
					return map;
				}
			}else{
				List<WmfRole> roles =  permissionService.getRolesPage(clickGroupId,start, limit);
				HashMap<String, Object> map = new HashMap<String, Object>();
				long total = permissionService.getRolesCount(clickGroupId);
				map.put("total", total);
				map.put("roles", roles);
				return map;
			}
			
	}
	
	/**
	 * 
	 * @Title: save 
	 * @Description: TODO(添加角色单数据，不含关联数据) 
	 * @param name
	 * @param enname
	 * @param status
	 * @param memo
	 * @param RID
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/role/save")
	public @ResponseBody Map<String,Object> save(
			@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "enname", required = false) String enname,
			@RequestParam(value = "status", required = false) Short status,
			@RequestParam(value = "memo", required = false) String memo,
			@RequestParam(value = "RID", required = false) String RID,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info(">>>>>>>>  /role/save" + name + "--->" + name);
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			if(null == id||id.equals("")){
				WmfRole wmfRole = new WmfRole();
				wmfRole.setName(name);
				wmfRole.setEnname(enname);
				wmfRole.setStatus((short) 1);
				wmfRole.setMemo(memo);
				int idValue = permissionService.insertWmfRole(wmfRole);
				result.put("success", Boolean.valueOf(true));
				result.put("id", idValue);
			}else{
				WmfRole wmfRole = new WmfRole();
				wmfRole.setId(id);
				wmfRole.setName(name);
				wmfRole.setEnname(enname);
				wmfRole.setStatus((short) 1);
				wmfRole.setMemo(memo);
				int f = permissionService.updateWmfRole(wmfRole);
				if(f>0){
					result.put("success", Boolean.valueOf(true));
					result.put("id", f);
				}else{
					result.put("failure", Boolean.valueOf(true));
					result.put("flag", Integer.valueOf(2));
				}
			}
		} catch (Exception e) {
			log.info(">>>>>>>>  /role/save" + e);
			e.printStackTrace();
			result.put("failure", Boolean.valueOf(true));
			result.put("flag", Integer.valueOf(2));
			result.put("msg", e.getMessage());
		}
		return result;
	}
	
	@RequestMapping(value = "/role/destroy")
	public @ResponseBody Map<String,Object> destroy(
			@RequestParam(value = "ids", required = true) String ids,
			@RequestParam(value = "RID", required = false) String RID){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			if (null == ids || ids.equals("")){
				result.put("failure" , Boolean.valueOf(true));
			}else{
				permissionService.deleteWmfRole(ids);
			}
			result.put("success" , Boolean.valueOf(true));
			result.put("updated", 1);
		} catch (Exception e) {
			result.put("failure" , Boolean.valueOf(true));
		}
		return result;
	}
	
	
	@Autowired
	private PermissionService permissionService;
}
